(function() {
  var access_log_stream, body_parser, check_permission, constant, cookie_parser, express, favicon, fs, iced, logger, merch_jobs, mkdirp, moment, multer, path, perm_path_prefixs, pg_sess, pgsql, rotator, session, session_options, settings, sysmenu, terms_jobs, webapp, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  fs = require('fs');

  path = require('path');

  favicon = require('serve-favicon');

  rotator = require('file-stream-rotator');

  logger = require('morgan');

  body_parser = require('body-parser');

  cookie_parser = require('cookie-parser');

  pgsql = require('pg');

  moment = require('moment');

  session = require('express-session');

  pg_sess = require('connect-pg-simple')(session);

  mkdirp = require('mkdirp');

  multer = require('multer');

  sysmenu = require('./sysmenu');

  constant = require('./lib/constant');

  merch_jobs = require('./lib/merch_jobs');

  terms_jobs = require('./lib/terms_jobs');

  webapp = express();

  module.exports = webapp;

  settings = require('./etc/settings');

  webapp.locals.web_logo = settings.web_logo;

  webapp.locals.web_title = settings.web_title;

  webapp.locals.web_title_color = settings.web_title_color;

  webapp.locals.web_subtitle = settings.web_subtitle;

  webapp.locals.web_subtitle_color = settings.web_subtitle_color;

  webapp.set('env', settings.env);

  webapp.locals.sysmenu = sysmenu;

  webapp.locals.constant = constant;

  global.settings = settings;

  global.constant = constant;

  webapp.set('trust proxy', settings.trust_proxy);

  webapp.set('views', path.join(__dirname, 'views'));

  webapp.set('view engine', 'jade');

  webapp.disable('x-powered-by');

  if (webapp.get('env') === 'development') {
    webapp.use(logger('dev'));
  } else {
    access_log_stream = rotator.getStream({
      date_format: 'YYYYMMDD',
      filename: settings.logs_dir + '/web_access-%DATE%.log',
      frequency: 'daily',
      verbose: true
    });
    webapp.use(logger('combined', {
      stream: access_log_stream
    }));
  }

  webapp.use(favicon(path.join(__dirname, 'public', settings.web_logo)));

  webapp.use(body_parser.json());

  webapp.use(body_parser.urlencoded({
    extended: true
  }));

  webapp.use(cookie_parser());

  webapp.use(express["static"](path.join(__dirname, 'public')));

  moment.fn.toJSON = function() {
    return this.format('YYYY-MM-DD HH:mm:ss');
  };

  pgsql.types.setTypeParser(1114, function(val) {
    if (val) {
      return moment(val);
    } else {
      return null;
    }
  });

  pgsql.types.setTypeParser(1184, function(val) {
    if (val) {
      return moment(val);
    } else {
      return null;
    }
  });

  pgsql.types.setTypeParser(1082, function(val) {
    if (val) {
      return moment(val);
    } else {
      return null;
    }
  });

  pgsql.types.setTypeParser(1083, function(val) {
    if (val) {
      return moment(val, 'HH:mm:ss');
    } else {
      return null;
    }
  });

  if (settings.master_server) {
    merch_jobs.add_all();
    terms_jobs.add_all();
  }

  session_options = {
    name: settings.session_name,
    secret: "Need Not Too Security",
    resave: false,
    saveUninitialized: false,
    rolling: true,
    cookie: {
      secure: false,
      maxAge: 60 * 60 * 1000
    },
    store: new pg_sess({
      pg: pgsql,
      conString: settings.pgsql_url,
      tableName: 'web_session'
    })
  };

  webapp.use(session(session_options));

  webapp.use(function(req, res, next) {
    res.locals.today = function() {
      return moment().format('YYYY-MM-DD');
    };
    res.locals.now = function() {
      return moment().format('YYYY-MM-DD HH:mm:ss');
    };
    res.locals.yesterday = function() {
      return moment().subtract(1, 'days').format('YYYY-MM-DD');
    };
    res.locals.fmtamt = function(yuan) {
      var num;
      if (!yuan) {
        return '0.00';
      }
      num = parseFloat(yuan);
      return num.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
    };
    res.locals.fmtamt2 = function(fen) {
      var num;
      if (!fen) {
        return '0.00';
      }
      num = parseInt(fen) / 100;
      return num.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
    };
    res.locals.fmttime10 = function(timestr) {
      if (!timestr) {
        return '';
      }
      return timestr.substring(0, 2) + '-' + timestr.substring(2, 4) + ' ' + timestr.substring(4, 6) + ':' + timestr.substring(6, 8) + ':' + timestr.substring(8, 10);
    };
    return next();
  });

  webapp.use('/login', require('./routes/login'));

  webapp.get('/logout', function(req, res, next) {
    return req.session.destroy(function(err) {
      return res.redirect('/login');
    });
  });

  webapp.use(function(req, res, next) {
    var userinfo;
    userinfo = req.session.userinfo;
    if (!userinfo || !userinfo.uuid) {
      if (req.xhr) {
        res.type('json');
        return res.json({
          succ: false,
          errmsg: '会话超时，请重新登录',
          redirect: '/login'
        });
      } else {
        req.session.last_access = req.url;
        return res.redirect("/login");
      }
    }
    res.locals.userinfo = userinfo;
    return next();
  });

  webapp.use(function(req, res, next) {
    var k, v, _ref;
    if (!req.body) {
      return next();
    }
    _ref = req.body;
    for (k in _ref) {
      v = _ref[k];
      if (typeof v === 'string' && req.body.hasOwnProperty(k)) {
        req.body[k] = v.trim();
      }
    }
    return next();
  });

  webapp.use(function(req, res, next) {
    var client, done, err, found, host, item, menu, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs = [];
    res.locals.breadcrumbs[0] = {
      'title': '主页',
      'href': '/'
    };
    (function(_this) {
      return (function(__iced_k) {
        if (!webapp.locals.hosts) {
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
            });
            pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  client = arguments[1];
                  return done = arguments[2];
                };
              })(),
              lineno: 170
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              return done(client && next(err));
            }
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
              });
              client.query("select * from pay_hosts order by serial", __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 173
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                return done(client && next(err));
              }
              done();
              return __iced_k(webapp.locals.hosts = result.rows);
            });
          });
        } else {
          return __iced_k();
        }
      });
    })(this)((function(_this) {
      return function() {
        var _i, _j, _len, _len1, _ref, _ref1;
        if (!req.session.host) {
          _ref = webapp.locals.hosts;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            host = _ref[_i];
            if (host.master) {
              req.session.host = host;
              break;
            }
          }
        } else {
          _ref1 = webapp.locals.hosts;
          for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
            host = _ref1[_j];
            if (host.uuid === req.session.host.uuid) {
              req.session.host = host;
              break;
            }
          }
        }
        if (!req.session.host) {
          req.session.host = webapp.locals.hosts[0];
        }
        res.locals.host = req.session.host;
        res.locals.shortcuts = req.session.shortcuts;
        (function(__iced_k) {
          if (!res.locals.shortcuts) {
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
              });
              pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    client = arguments[1];
                    return done = arguments[2];
                  };
                })(),
                lineno: 196
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                return done(client && next(err));
              }
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
                });
                client.query("select shortcuts from web_users where uuid = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return result = arguments[1];
                    };
                  })(),
                  lineno: 201
                }));
                __iced_deferrals._fulfill();
              })(function() {
                if (err) {
                  return done(client && next(err));
                }
                done();
                if (result.rows.length > 0) {
                  req.session.shortcuts = result.rows[0].shortcuts;
                }
                if (!req.session.shortcuts) {
                  req.session.shortcuts = [];
                }
                return __iced_k(res.locals.shortcuts = req.session.shortcuts);
              });
            });
          } else {
            return __iced_k();
          }
        })(function() {
          var _k, _l, _len2, _len3, _ref2;
          res.locals.keyword = req.body.keyword || req.query.keyword || '';
          res.locals.page_no = parseInt(req.body.page_no) || 1;
          res.locals.page_rows = req.body.page_rows || parseInt(req.cookies['table.page_rows']) || 10;
          res.locals.offset = (res.locals.page_no - 1) * res.locals.page_rows;
          res.locals.total = 0;
          res.locals.page_num = 1;
          found = false;
          for (_k = 0, _len2 = sysmenu.length; _k < _len2; _k++) {
            menu = sysmenu[_k];
            if (found) {
              break;
            }
            if (menu.href) {
              if (req.path === menu.href || req.path.startsWith(menu.href + '/')) {
                res.locals.breadcrumbs.push({
                  'title': menu.header,
                  'href': menu.href
                });
              }
            }
            _ref2 = menu.items;
            for (_l = 0, _len3 = _ref2.length; _l < _len3; _l++) {
              item = _ref2[_l];
              if (req.path.startsWith(item.href)) {
                res.locals.breadcrumbs.push({
                  'title': item.title,
                  'href': item.href
                });
                res.locals.search_form_action = req.path;
                found = true;
                break;
              }
            }
          }
          return next();
        });
      };
    })(this));
  });

  webapp.set('notify_modified_time', moment().format('x'));

  webapp.use(function(req, res, next) {
    var ses_modified_time, sys_modified_time;
    if (req.session.notify_modified_time && req.session.notify) {
      sys_modified_time = webapp.get('notify_modified_time');
      ses_modified_time = req.session.notify_modified_time;
      if (parseInt(sys_modified_time) < parseInt(ses_modified_time)) {
        res.locals.notify = req.session.notify;
        req.session_modified = false;
        return next();
      }
    }
    req.session.notify = {};
    req.session_modified = true;
    req.session.notify_modified_time = moment().format('x');
    return next();
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.messages) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 261
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select serial from web_messages where status = 1 and recver = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 267
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.messages = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.messages_send) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 277
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select serial from web_messages where status = 1 and sender = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 283
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.messages_send = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 291
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select serial from web_notify where status = 1 order by serial", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 296
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.notifications = result.rows;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
            });
            client.query("select ( select count(*)::int > 0 from web_notify where status = 1 and level > 1 ) as warning, ( select count(*)::int > 0 from web_notify where status = 1 and level > 2 ) as error", __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 307
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            req.session.notify.warning = result.rows[0].warning;
            req.session.notify.error = result.rows[0].error;
            return next();
          });
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, perm, result, wizcheck_check_perm, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.wizard_wait_check) {
      return next();
    }
    wizcheck_check_perm = false;
    if (req.session.userinfo.role_root) {
      wizcheck_check_perm = true;
    } else {
      _ref = req.session.userinfo.permission;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        perm = _ref[_i];
        if (perm.href === '/wizcheck') {
          if (perm.permission.access && perm.permission.check) {
            wizcheck_check_perm = true;
          }
          break;
        }
      }
    }
    if (!wizcheck_check_perm) {
      req.session.notify.wizard_wait_check = [];
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 334
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select w.uuid from pay_wizard as w left join pay_merch_ex as m1 on m1.uuid = w.merid1 left join pay_terms_ex as t1 on t1.uuid = w.termid1 left join pay_merch_ex as m2 on m2.uuid = w.merid2 left join pay_terms_ex as t2 on t2.uuid = w.termid2 where w.inputer <> $1 and case when w.merch_type in (1, 2) then w.merid1  in (select uuid from pay_merch_ex where action = 1) and w.termid1 in (select uuid from pay_terms_ex where action = 1) and w.merid2  in (select uuid from pay_merch_ex where action = 1) and w.termid2 in (select uuid from pay_terms_ex where action = 1) and m1.rejected = false and t1.rejected = false and m2.rejected = false and t2.rejected = false end", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 353
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.wizard_wait_check = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, merch_check_perm, perm, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.merchs_wait_check) {
      return next();
    }
    merch_check_perm = false;
    if (req.session.userinfo.role_root) {
      merch_check_perm = true;
    } else {
      _ref = req.session.userinfo.permission;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        perm = _ref[_i];
        if (perm.href === '/merch') {
          if (perm.permission.access && perm.permission.check) {
            merch_check_perm = true;
          }
          break;
        }
      }
    }
    if (!merch_check_perm) {
      req.session.notify.merchs_wait_check = [];
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 379
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_merch_ex where checked = false and rejected = false and inputer <> $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 386
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.merchs_wait_check = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.merchs_rejected) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 396
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_merch_ex where rejected = true and inputer = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 402
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.merchs_rejected = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.merchs_checked) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 412
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_merch_ex where checked = true and inputer = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 418
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.merchs_checked = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.merchs_input) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 428
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_merch_ex where rejected = false and checked = false and inputer = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 435
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.merchs_input = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.merchs_wait_effect) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 445
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_merch_ex where checked = true and checker = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 451
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.merchs_wait_effect = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, perm, result, terms_check_perm, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.terms_wait_check) {
      return next();
    }
    terms_check_perm = false;
    if (req.session.userinfo.role_root) {
      terms_check_perm = true;
    } else {
      _ref = req.session.userinfo.permission;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        perm = _ref[_i];
        if (perm.href === '/terms') {
          if (perm.permission.access && perm.permission.check) {
            terms_check_perm = true;
          }
          break;
        }
      }
    }
    if (!terms_check_perm) {
      req.session.notify.terms_wait_check = [];
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 476
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_terms_ex where checked = false and rejected = false and inputer <> $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 483
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.terms_wait_check = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.terms_rejected) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 493
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_terms_ex where rejected = true and inputer = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 499
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.terms_rejected = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.terms_input) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 509
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_terms_ex where rejected = false and checked = false and inputer = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 516
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.terms_input = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.terms_wait_effect) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 526
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_terms_ex where checked = true and checker = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 532
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.terms_wait_effect = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, p1_check_perm, perm, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.p1_wait_check) {
      return next();
    }
    p1_check_perm = false;
    if (req.session.userinfo.role_root) {
      p1_check_perm = true;
    } else {
      _ref = req.session.userinfo.permission;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        perm = _ref[_i];
        if (perm.href === '/p1') {
          if (perm.permission.access && perm.permission.check) {
            p1_check_perm = true;
          }
          break;
        }
      }
    }
    if (!p1_check_perm) {
      req.session.notify.p1_wait_check = [];
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 558
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_p1 where checked = false and inputer <> $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 564
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.p1_wait_check = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.p1_input) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 575
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_p1 where checked = false and inputer = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 581
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.p1_input = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, p2_check_perm, perm, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.p2_wait_check) {
      return next();
    }
    p2_check_perm = false;
    if (req.session.userinfo.role_root) {
      p2_check_perm = true;
    } else {
      _ref = req.session.userinfo.permission;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        perm = _ref[_i];
        if (perm.href === '/p2') {
          if (perm.permission.access && perm.permission.check) {
            p2_check_perm = true;
          }
          break;
        }
      }
    }
    if (!p2_check_perm) {
      req.session.notify.p2_wait_check = [];
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 607
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_p2 where checked = false and inputer <> $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 613
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.p2_wait_check = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (req.session.notify.p2_input) {
      return next();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 624
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/webapp.coffee"
          });
          client.query("select uuid from pay_p2 where checked = false and inputer = $1", [req.session.userinfo.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 630
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          req.session.notify.p2_input = result.rows;
          done();
          return next();
        });
      };
    })(this));
  });

  webapp.use(function(req, res, next) {
    if (!req.session_modified) {
      return next();
    }
    return req.session.save(function(err) {
      res.locals.notify = req.session.notify;
      return next();
    });
  });

  webapp.use(function(req, res, next) {
    var item, menu, perm, permission, _i, _j, _k, _len, _len1, _len2, _ref;
    if (req.session.userinfo.role_root) {
      return next();
    }
    permission = req.session.userinfo.permission;
    if (!permission) {
      return req.session.destroy(function(err) {
        return res.redirect("/login");
      });
    }
    for (_i = 0, _len = sysmenu.length; _i < _len; _i++) {
      menu = sysmenu[_i];
      _ref = menu.items;
      for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
        item = _ref[_j];
        item.permission = [];
        for (_k = 0, _len2 = permission.length; _k < _len2; _k++) {
          perm = permission[_k];
          if (perm.href === item.href) {
            item.permission = perm.permission;
            break;
          }
        }
      }
    }
    return next();
  });

  webapp.use(function(req, res, next) {
    res.append('Cache-Control', 'must-revalidate');
    res.append('max-age', '10');
    return next();
  });

  webapp.use('/', require('./routes/index'));

  webapp.use('/userinfo', require('./routes/userinfo'));

  webapp.use('/notify', require('./routes/notify'));

  webapp.use('/message', require('./routes/message'));

  webapp.use('/i', require('./routes/i'));

  webapp.use('/o', require('./routes/o'));

  perm_path_prefixs = ['new', 'edit', 'delete', 'disable', 'check', 'import', 'export', 'batch', 'recharge'];

  check_permission = function(permission, subpath, href) {
    var k, _i, _len;
    if (!permission || !permission.access) {
      return false;
    }
    if (subpath.startsWith('/')) {
      subpath = subpath.substring(1);
    }
    for (_i = 0, _len = perm_path_prefixs.length; _i < _len; _i++) {
      k = perm_path_prefixs[_i];
      if (subpath === k || subpath.startsWith(k + '/')) {
        return permission[k];
      }
    }
    if (subpath === 'input' || subpath.startsWith('input/')) {
      return permission.edit;
    }
    if (subpath === 'enable' || subpath.startsWith('enable/')) {
      return permission.disable;
    }
    return true;
  };

  webapp.use(function(req, res, next) {
    var item, k, menu, page_perms, perm, permission, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref;
    if (req.session.userinfo.role_root) {
      res.locals.page_perms = {
        root: true
      };
      for (_i = 0, _len = perm_path_prefixs.length; _i < _len; _i++) {
        k = perm_path_prefixs[_i];
        res.locals.page_perms[k] = true;
      }
      res.locals.page_perms_str = JSON.stringify(res.locals.page_perms);
      return next();
    }
    permission = req.session.userinfo.permission;
    if (!permission) {
      return req.session.destroy(function(err) {
        return res.redirect("/login");
      });
    }
    for (_j = 0, _len1 = sysmenu.length; _j < _len1; _j++) {
      menu = sysmenu[_j];
      _ref = menu.items;
      for (_k = 0, _len2 = _ref.length; _k < _len2; _k++) {
        item = _ref[_k];
        if (!req.path.startsWith(item.href)) {
          continue;
        }
        for (_l = 0, _len3 = permission.length; _l < _len3; _l++) {
          perm = permission[_l];
          if (perm.href !== item.href) {
            continue;
          }
          if (!check_permission(perm.permission, req.path.substring(item.href.length), item.href)) {
            if (req.xhr) {
              res.type('json');
            }
            return next(new Error('权限拒绝，请联系系统管理人员'));
          }
          page_perms = perm.permission;
          page_perms.root = req.session.userinfo.role_root;
          res.locals.page_perms = page_perms;
          res.locals.page_perms_str = JSON.stringify(page_perms);
          return next();
        }
        if (req.xhr) {
          res.type('json');
        }
        return next(new Error('权限拒绝，请联系系统管理人员'));
      }
    }
    return next(new Error('拒绝访问，您访问的地址不在系统服务范围内'));
  });

  webapp.use('/i/091', require('./routes/i/091'));

  webapp.use('/i/096', require('./routes/i/096'));

  webapp.use('/i/351', require('./routes/i/351'));

  webapp.use('/o/091', require('./routes/o/091'));

  webapp.use('/o/096', require('./routes/o/096'));

  webapp.use('/o/351', require('./routes/o/351'));

  webapp.use('/o/071', require('./routes/o/071'));

  webapp.use('/wizard', require('./routes/wizard'));

  webapp.use('/wizcheck', require('./routes/wizcheck'));

  webapp.use('/merch', require('./routes/merch'));

  webapp.use('/terms', require('./routes/terms'));

  webapp.use('/p1', require('./routes/p1'));

  webapp.use('/p2', require('./routes/p2'));

  webapp.use('/p2/fund', require('./routes/p2/fund'));

  webapp.use('/trlist', require('./routes/trlist'));

  webapp.use('/trstat/trcodes', require('./routes/trstat/trcodes'));

  webapp.use('/trstat/merch', require('./routes/trstat/merch'));

  webapp.use('/trstat/p1_order', require('./routes/trstat/p1_order'));

  webapp.use('/trstat/p1_order_sum', require('./routes/trstat/p1_order_sum'));

  webapp.use('/trstat/p2_order', require('./routes/trstat/p2_order'));

  webapp.use('/trstat/p2_order_sum', require('./routes/trstat/p2_order_sum'));

  webapp.use('/trstat/p2_fund_sum', require('./routes/trstat/p2_fund_sum'));

  webapp.use('/trstat', require('./routes/trstat'));

  webapp.use('/t40_recvr/done', require('./routes/t40_recvr/done'));

  webapp.use('/t40_recvr', require('./routes/t40_recvr'));

  webapp.use('/reports', require('./routes/reports'));

  webapp.use('/upay/acoma', require('./routes/upay/acoma'));

  webapp.use('/trctl_t', require('./routes/trctl_t'));

  webapp.use('/fee/set', require('./routes/fee/set'));

  webapp.use('/fee', require('./routes/fee'));

  webapp.use('/secure', require('./routes/secure'));

  webapp.use('/hosts', require('./routes/hosts'));

  webapp.use('/users', require('./routes/users'));

  webapp.use('/roles', require('./routes/roles'));

  webapp.use('/email', require('./routes/email'));

  webapp.use('/cardbin', require('./routes/cardbin'));

  webapp.use('/settings', require('./routes/settings'));

  webapp.use('/sched', require('./routes/sched'));

  webapp.use('/logs', require('./routes/logs'));

  webapp.use('/audit', require('./routes/audit'));

  webapp.use('/utils', require('./routes/utils'));

  webapp.use('/upgrade', require('./routes/upgrade'));

  webapp.use(function(req, res, next) {
    var err;
    err = new Error('页面不存在');
    err.status = 404;
    return next(err);
  });

  if (webapp.get('env') === 'development') {
    webapp.use(function(err, req, res, next) {
      console.log(err);
      return next(err);
    });
  }

  webapp.use(function(err, req, res, next) {
    var message;
    res.status(err.status || 500);
    if (err.code === '23503') {
      message = "操作违反外键约束，这通常是尝试去删除一条记录，" + "但这条记录尚被其它表关联引用，因此不能删除，" + "也有可能是其它原因导致，具体请查看错误日志";
    }
    if (res.get('Content-Type') === 'application/json') {
      res.status(200);
      return res.json({
        succ: false,
        errmsg: message || err.message
      });
    }
    if (!message) {
      message = err.message;
    }
    if (message.length < 20) {
      err.message = '';
    } else {
      message = '系统运行时异常';
    }
    return res.render('error', {
      message: message,
      error: err
    });
  });

}).call(this);
